package com.desire.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@SpringBootTest
public class Redis_Scan {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void write() throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            executorService.execute(() -> {
                for (int e = 0; e < 1000; e++) {
                    stringRedisTemplate.opsForValue().set("a" + e, String.valueOf(e));
                }

                for (int e = 0; e < 1000; e++) {
                    stringRedisTemplate.opsForValue().set("b" + e, String.valueOf(e));
                }
                for (int e = 0; e < 1000; e++) {
                    stringRedisTemplate.opsForValue().set("c" + e, String.valueOf(e));
                }
            });
        }
        Thread.sleep(100 * 1000);
    }

    @Test
    void Scan() {
        ScanOptions build = ScanOptions.scanOptions().match("b*").count(50).build();
        Cursor<String> cursor = stringRedisTemplate.scan(build);
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
        cursor.close();
    }
}
